package com.pointi.emoTools;

import java.sql.ResultSet;

public class evCallDataAverage
{
	public int call_count ;
	public double ec_cai ;
	public double ec_cai_sd ;
	public double ec_aai ;
	public double ec_aai_sd ;
	public double ht_dur ;
	public double ht_dur_sd ;
	public double ht_ht ;
	public double ht_ht_sd ;
	public double ht_ctt ;
	public double ht_ctt_sd ;
	public double ht_att ;
	public double ht_att_sd ;
	public double ht_cttr ;
	public double ht_cttr_sd ;
	public double ht_attr ;
	public double ht_attr_sd ;
	public double lr_lro ;
	public double lr_lro_sd ;
	public double lr_lrg ;
	public double lr_lrg_sd ;
	public double lr_lror ;
	public double lr_lror_sd ;
	public double lr_lrgr ;
	public double lr_lrgr_sd ;

	public evCallDataAverage()
	{
		call_count = 0 ;
		ec_cai = 0 ;
		ec_cai_sd = 0 ;
		ec_aai = 0 ;
		ec_aai_sd = 0 ;
		ht_dur = 0 ;
		ht_dur_sd = 0 ;
		ht_ht = 0 ;
		ht_ht_sd = 0 ;
		ht_ctt = 0 ;
		ht_ctt_sd = 0 ;
		ht_att = 0 ;
		ht_att_sd = 0 ;
		ht_cttr = 0 ;
		ht_cttr_sd = 0 ;
		ht_attr = 0 ;
		ht_attr_sd = 0 ;
		lr_lro = 0 ;
		lr_lro_sd = 0 ;
		lr_lrg = 0 ;
		lr_lrg_sd = 0 ;
		lr_lror = 0 ;
		lr_lror_sd = 0 ;
		lr_lrgr = 0 ;
		lr_lrgr_sd = 0 ;
	}

	public void fnSetParameter(ResultSet inRS)
	{
		try
		{
			call_count = inRS.getInt("call_count") ;
			ec_cai = inRS.getDouble("ec_cai") ;
			ec_cai_sd = inRS.getDouble("ec_cai_sd") ;
			ec_aai = inRS.getDouble("ec_aai") ;
			ec_aai_sd = inRS.getDouble("ec_aai_sd") ;
			ht_dur = inRS.getDouble("ht_dur") ;
			ht_dur_sd = inRS.getDouble("ht_dur_sd") ;
			ht_ht = inRS.getDouble("ht_ht") ;
			ht_ht_sd = inRS.getDouble("ht_ht_sd") ;
			ht_ctt = inRS.getDouble("ht_ctt") ;
			ht_ctt_sd = inRS.getDouble("ht_ctt_sd") ;
			ht_att = inRS.getDouble("ht_att") ;
			ht_att_sd = inRS.getDouble("ht_att_sd") ;
			ht_cttr = inRS.getDouble("ht_cttr") ;
			ht_cttr_sd = inRS.getDouble("ht_cttr_sd") ;
			ht_attr = inRS.getDouble("ht_attr") ;
			ht_attr_sd = inRS.getDouble("ht_attr_sd") ;
			lr_lro = inRS.getDouble("lr_lro") ;
			lr_lro_sd = inRS.getDouble("lr_lro_sd") ;
			lr_lrg = inRS.getDouble("lr_lrg") ;
			lr_lrg_sd = inRS.getDouble("lr_lrg_sd") ;
			lr_lror = inRS.getDouble("lr_lror") ;
			lr_lror_sd = inRS.getDouble("lr_lror_sd") ;
			lr_lrgr = inRS.getDouble("lr_lrgr") ;
			lr_lrgr_sd = inRS.getDouble("lr_lrgr_sd") ;
		}
		catch (Exception e) { e.printStackTrace() ; }
	}
	
	// Fill Call Data SD (Average)
	public void fnSetCallDataSP(ResultSet inRS)
	{
		try
		{
			call_count = inRS.getInt("cnt") ;
			ec_cai = inRS.getDouble("ec_cai") ;
			ec_aai = inRS.getDouble("ec_aai") ;
			ht_dur = inRS.getDouble("ht_dur") ;
			ht_ht = inRS.getDouble("ht_ht") ;
			ht_ctt = inRS.getDouble("ht_ctt") ;
			ht_att = inRS.getDouble("ht_att") ;
			ht_cttr = inRS.getDouble("ht_cttr") ;
			ht_attr = inRS.getDouble("ht_attr") ;
			lr_lro = inRS.getDouble("lr_lro") ;
			lr_lrg = inRS.getDouble("lr_lrg") ;
			lr_lror = inRS.getDouble("lr_lror") ;
			lr_lrgr = inRS.getDouble("lr_lrgr") ;
		}
		catch(Exception e) { e.printStackTrace() ; }
	}
	
	// Fill Call Data SP (Standart Deviation)
	public void fnSetCallDataSD(ResultSet inRS)
	{
		try
		{
			while (inRS.next())
			{
				ec_cai_sd += Math.pow(ec_cai - inRS.getDouble("ec_cai"), 2) ;
				ec_aai_sd += Math.pow(ec_aai - inRS.getDouble("ec_aai"), 2) ;
				ht_dur_sd += Math.pow(ht_dur - inRS.getDouble("ht_dur"), 2) ;
				ht_ht_sd += Math.pow(ht_ht - inRS.getDouble("ht_ht"), 2) ;
				ht_ctt_sd += Math.pow(ht_ctt - inRS.getDouble("ht_ctt"), 2) ;
				ht_att_sd += Math.pow(ht_att - inRS.getDouble("ht_att"), 2) ;
				ht_cttr_sd += Math.pow(ht_cttr - inRS.getDouble("ht_cttr"), 2) ;
				ht_attr_sd += Math.pow(ht_attr - inRS.getDouble("ht_attr"), 2) ;
				lr_lro_sd += Math.pow(lr_lro - inRS.getDouble("lr_lro"), 2) ;
				lr_lrg_sd += Math.pow(lr_lrg - inRS.getDouble("lr_lrg"), 2) ;
				lr_lror_sd += Math.pow(ec_cai - inRS.getDouble("lr_lror"), 2) ;
				lr_lrgr_sd += Math.pow(ec_cai - inRS.getDouble("lr_lrgr"), 2) ;
			}
			ec_cai_sd = Math.sqrt(ec_cai_sd / call_count) ;
			ec_aai_sd = Math.sqrt(ec_aai_sd / call_count) ;
			ht_dur_sd = Math.sqrt(ht_dur_sd / call_count) ;
			ht_ht_sd = Math.sqrt(ht_ht_sd / call_count) ;
			ht_ctt_sd = Math.sqrt(ht_ctt_sd / call_count) ;
			ht_att_sd = Math.sqrt(ht_att_sd / call_count) ;
			ht_cttr_sd = Math.sqrt(ht_cttr_sd / call_count) ;
			ht_attr_sd = Math.sqrt(ht_attr_sd / call_count) ;
			lr_lro_sd = Math.sqrt(lr_lro_sd / call_count) ;
			lr_lrg_sd = Math.sqrt(lr_lrg_sd / call_count) ;
			lr_lror_sd = Math.sqrt(lr_lror_sd / call_count) ;
			lr_lrgr_sd = Math.sqrt(lr_lrgr_sd / call_count) ;
		}
		catch(Exception e) { e.printStackTrace() ; }
	}
	
	public void fnMergeCallDataAVG(evCallDataAverage inAVG)
	{
		int CNTnew = call_count ;
		int CNTold = inAVG.call_count ;
		int CNTall = call_count + inAVG.call_count ;
		
		call_count = CNTall ;
		ec_cai = ((ec_cai * CNTold) + (inAVG.ec_cai * CNTnew)) / CNTall ;
		ec_aai = ((ec_aai * CNTold) + (inAVG.ec_aai * CNTnew)) / CNTall ;
		ht_dur = ((ht_dur * CNTold) + (inAVG.ht_dur * CNTnew)) / CNTall ;
		ht_ht = ((ht_ht * CNTold) + (inAVG.ht_ht * CNTnew)) / CNTall ;
		ht_ctt = ((ht_ctt * CNTold) + (inAVG.ht_ctt * CNTnew)) / CNTall ;
		ht_att = ((ht_att * CNTold) + (inAVG.ht_att * CNTnew)) / CNTall ;
		ht_cttr = ((ht_cttr * CNTold) + (inAVG.ht_cttr * CNTnew)) / CNTall ;
		ht_attr = ((ht_attr * CNTold) + (inAVG.ht_attr * CNTnew)) / CNTall ;
		lr_lro = ((lr_lro * CNTold) + (inAVG.lr_lro * CNTnew)) / CNTall ;
		lr_lrg = ((lr_lrg * CNTold) + (inAVG.lr_lrg * CNTnew)) / CNTall ;
		lr_lror = (lr_lror * CNTold) + (inAVG.lr_lror * CNTnew) / CNTall ;
		lr_lrgr = (lr_lrgr * CNTold) + (inAVG.lr_lrgr * CNTnew) / CNTall ;
	}
	
	public void fnMergeCallDataSD(evCallDataAverage inAVG)
	{
		int CNTnew = call_count ;
		int CNTold = inAVG.call_count ;
		int CNTall = call_count + inAVG.call_count ;
		
		call_count = CNTall ;
		ec_cai_sd = ((ec_cai_sd * CNTold) + (inAVG.ec_cai_sd * CNTnew)) / CNTall ;
		ec_aai_sd = ((ec_aai_sd * CNTold) + (inAVG.ec_aai_sd * CNTnew)) / CNTall ;
		ht_dur_sd = ((ht_dur_sd * CNTold) + (inAVG.ht_dur_sd * CNTnew)) / CNTall ;
		ht_ht_sd = ((ht_ht_sd * CNTold) + (inAVG.ht_ht_sd * CNTnew)) / CNTall ;
		ht_ctt_sd = ((ht_ctt_sd * CNTold) + (inAVG.ht_ctt_sd * CNTnew)) / CNTall ;
		ht_att_sd = ((ht_att_sd * CNTold) + (inAVG.ht_att_sd * CNTnew)) / CNTall ;
		ht_cttr_sd = ((ht_cttr_sd * CNTold) + (inAVG.ht_cttr_sd * CNTnew)) / CNTall ;
		ht_attr_sd = ((ht_attr_sd * CNTold) + (inAVG.ht_attr_sd * CNTnew)) / CNTall ;
		lr_lro_sd = ((lr_lro_sd * CNTold) + (inAVG.lr_lro_sd * CNTnew)) / CNTall ;
		lr_lrg_sd = ((lr_lrg_sd * CNTold) + (inAVG.lr_lrg_sd * CNTnew)) / CNTall ;
		lr_lror_sd = (lr_lror_sd * CNTold) + (inAVG.lr_lror_sd * CNTnew) / CNTall ;
		lr_lrgr_sd = (lr_lrgr_sd * CNTold) + (inAVG.lr_lrgr_sd * CNTnew) / CNTall ;
	}
	
	public String fnGetQuery(String inWorkDate)
	{
		String query = "" ;
		query = " INSERT INTO t_ev_call_df_data_average ( " +
				" work_date , " +
				" call_count , " +
				" ec_cai , " +
				" ec_cai_sd , " +
				" ec_aai , " +
				" ec_aai_sd , " +
				" ht_dur , " +
				" ht_dur_sd , " +
				" ht_ht , " +
				" ht_ht_sd , " +
				" ht_ctt , " +
				" ht_ctt_sd , " +
				" ht_att , " +
				" ht_att_sd , " +
				" ht_cttr , " +
				" ht_cttr_sd , " +
				" ht_attr , " +
				" ht_attr_sd , " +
				" lr_lro , " +
				" lr_lro_sd , " +
				" lr_lrg , " +
				" lr_lrg_sd , " +
				" lr_lror , " +
				" lr_lror_sd , " +
				" lr_lrgr , " +
				" lr_lrgr_sd " +
				" ) VALUES ( " + 
				 " '" + inWorkDate + "' , " +
				 call_count + " , " +
				 ec_cai + " , " +
				 ec_cai_sd + " , " +
				 ec_aai + " , " +
				 ec_aai_sd + " , " +
				 ht_dur + " , " +
				 ht_dur_sd + " , " +
				 ht_ht + " , " +
				 ht_ht_sd + " , " +
				 ht_ctt + " , " +
				 ht_ctt_sd + " , " +
				 ht_att + " , " +
				 ht_att_sd + " , " +
				 ht_cttr + " , " +
				 ht_cttr_sd + " , " +
				 ht_attr + " , " +
				 ht_attr_sd + " , " +
				 lr_lro + " , " +
				 lr_lro_sd + " , " +
				 lr_lrg + " , " +
				 lr_lrg_sd + " , " +
				 lr_lror + " , " +
				 lr_lror_sd + " , " +
				 lr_lrgr + " , " +
				 lr_lrgr_sd +
				 " ) ; " ;
		return query ;
	}
}
